package com.musicdaze.utils.math;

/**
 * Represents a vector with 3 values
 * @author mfujihara
 * 
 */
public class Vector4 {
  /** x value **/
  public float x;
  /** y value **/
  public float y;
  /** z value **/
  public float z;
  /** t value **/
  public float t;

  /**
   * public constructor that sets x=y=z=t=0
   */
  public Vector4() {
    this.x = 0;
    this.y = 0;
    this.z = 0;
    this.t = 0;
  }

  /**
   * public contructor that sets x y z and t
   * @param x value to set x as
   * @param y value to set y as
   * @param z value to set z as
   * @param t value to set t as
   */
  public Vector4(float x, float y, float z, float t) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.t = t;
  }

  /**
   * adds v to this vector. mutate
   * @param v the vector to add to this
   * @return this
   */
  public Vector4 add(Vector4 v) {
    this.x += v.x;
    this.y += v.y;
    this.z += v.z;
    this.t += v.t;
    return this;
  }

  /**
   * sets this with values from another vector. mutate
   * @param as the vector to set values from
   */
  public void set(Vector4 as) {
    this.x = as.x;
    this.y = as.y;
    this.z = as.z;
    this.t = as.t;
  }
  /**
   * dot product
   * @param v the vector to dot with
   * @return the dot product
   */
  public float dot(Vector4 v){
    return x*v.x + y*v.y + z*v.z + t*v.t;
  }

  /**
   * Returns the magnitude of this vector
   * @return the magnitude of this vector
   */
  public float mag() {
    return (float) Math.sqrt(x * x + y * y + z * z + t * t);
  }

  /**
   * Normalizes this vector.
   * @return this
   */
  public Vector4 normalize() {
    this.scale(1 / mag());
    return this;
  }

  /**
   * scales this vector by a value. mutate
   * @param s the value to scale this as
   * @return this
   */
  public Vector4 scale(float s) {
    this.x *= s;
    this.y *= s;
    this.z *= s;
    this.t *= t;
    return this;
  }

  /**
   * gets the distance from this vector to point assuming both are points in 4d
   * space
   * @param point the point to find the distance to
   * @return the distance between this and point
   */
  public float distanceTo(Vector4 point) {
    Vector4 diff = Vector4.sub(this, point);
    return diff.mag();
  }

  /**
   * compares x y and z
   * @param o the object to check equality with
   * @return true if this equals the object based on x, y, z,t; false otherwise
   */
  public boolean equals(Object o) {
    if (o instanceof Vector4) {
      Vector4 v = (Vector4) o;
      return v.x == this.x && v.y == this.y && this.z == v.z && this.t == v.t;
    }
    return false;
  }

  /**
   * uses (int)(x + y + z + t)
   * @return hashcode
   */
  public int hashCode() {
    return (int) (x + y + z + t);
  }

  /**
   * prints out (x,y,z,t)
   * @return (x,y,z,t)
   */
  @Override
  public String toString() {
    return "(" + this.x + "," + this.y + "," + this.z + "," + this.t + ")";
  }

  /**
   * adds two vectors together without mutating them.
   * @param a a vector to add
   * @param b a vector to add
   * @return a vector that represents the sum of a and b
   */
  public static Vector4 add(Vector4 a, Vector4 b) {
    Vector4 ret = new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.t + b.t);
    return ret;
  }

  /**
   * subtracts two vectors without mutation.
   * @param a a vector to subtract from
   * @param b a vector to subtract
   * @return Vector2 that is a-b
   */
  public static Vector4 sub(Vector4 a, Vector4 b) {
    Vector4 ret = new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.t - b.t);
    return ret;
  }
  /**
   * Gets the angle between a to b
   * @param a the start vector
   * @param b the end vector
   * @return the angle between two vectors
   */
  public static float angleBetween(Vector4 a, Vector4 b){
    float dot = a.dot(b);
    dot = dot/a.mag();
    dot = dot/b.mag();
    return (float) Math.acos(dot);
  }
}
